﻿using System;
using System.Collections.Generic;
using System.Text;
using Portal.Language;
using Portal.Environment;
using Portal.Interface;

namespace Portal.CDSP
{
    public class UpdateFAskQuestionsWP : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.UpdateFAskQuestionsWP";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Cập nhật FAQ");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }



        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CDSP.UpdateFAskQuestionsWP));
            string AskID = System.Web.HttpContext.Current.Request["AskID"];
            FAskQuestionsCls FAQCls = new FAskQuestionsBLL().CreateModel(Environments.ActiveLanguage, AskID);
            if (FAQCls == null)
            {
                return Environments.GetRecordNotFound();
            }

            
            string SelectLanguageText =
           "<select id=divSelectLanguage onchange=javascript:DetectUpdateOnlineSupportBody('" + AskID + "');>";
            string[] Languages = Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";
            string status = null; ;
            if (FAQCls.AskStatus)
                status = "checked"; 
            string RetHtml =               
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CDSP", "QuestionsWP.js") +
                             Environments.GetFormDialog(
                             //" <script type=\"text/javascript\"src=\"SODC-vie/Scripts/FAskQuestions.js\" ></script>" +
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateFAQFormLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>" + SelectLanguageText + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateAskBy", "Họ tên:") + "</td>" +
                             "              <td><input type = text readonly class=cssTextBoxDialog id=txtAskBy value=\"" + FAQCls.AskBy + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId,"UpdateAsk", "Câu hỏi") + "</td>" +
                             "              <td><input type = text readonly class=cssTextBoxDialog id=txtAskAnswer value=\"" + FAQCls.AskContent + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "TimeAsk", "Ngày") + "</td>" +
                             "              <td><input type = text class=cssTextBoxDialog readonly id=txtDate value=\"" + FAQCls.AskTime + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateAnswer", "Trả lời") + "</td>" +
                             "              <td><textarea class=cssTextBoxDialog id=txtAnswer rows = 15>" + FAQCls.AskAnswer + "</textarea></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateStatus", "Trạng thái") + "</td>" +
                             "               <td>Hiện <INPUT TYPE=checkbox id= chkStatus "+ status + " value =\"" + status + "\">" +                                       
                             //"              <td><input type = radio name = group1 "+status+"  value = true>Hiện <input type = radio name = group1 value = false>Ẩn</td>" +
                             "              </td>" +
                            
                             "          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             
                             "              <td><input type=button class=cssSaveButton onclick=javascript:SaveUpdateFAQ('" + AskID + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateFAQSaveButton", "Lưu") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateOnlineSupportCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>", ResourceBLL.GetText(WebPartId, "UpdateOnlineSupportFormTitle", "Cập nhật FAQ"));
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateFAQ(string ActiveLanguage,string AskID,string AskAnswer,bool AskStatus)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                if (AskAnswer.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "EnterAnswer", "Nhập câu trả lời"));
                new FAskQuestionsBLL().ServerSideUpdateFAQ(null, ActiveLanguage, AskID,AskAnswer,AskStatus);
                return ResourceBLL.GetText(WebPartId, "MessageUpdateOk", "Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        public override bool AllowFrontService
        {
            get
            {
                return false;
            }
        }

        
    }
}
